import {
	DEPortalViewActionType,
	IDEPortalViewAbility,
	IDEPortalViewController,
	IDEPortalViewStore,
	DashboardActionType,
	IParam,
	ISearchFormAbility,
	IDashboardAbility,
	SearchFormActionType,
	IViewActionResult,
	IDEPortalViewControllerParams,
} from '@/core/interface';
import { deepCopy } from '@/core/utils';
import { DEViewController } from './de-view-controller';

/**
 * 实体数据看板视图
 *
 * @export
 * @class DEPortalViewController
 * @extends {DEViewController<DEPortalViewActionType, IDEPortalViewStore, IDEPortalViewAbility>}
 * @implements {IDEPortalViewController<IDEPortalViewStore, IDEPortalViewAbility>}
 */
export class DEPortalViewController
	extends DEViewController<
		DEPortalViewActionType,
		IDEPortalViewStore,
		IDEPortalViewAbility
	>
	implements IDEPortalViewController<IDEPortalViewStore, IDEPortalViewAbility>
{
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {(IDEPortalViewControllerParams<DEPortalViewActionType, IDEPortalViewAbility>)} params
	 * @memberof DEPortalViewController
	 */
	protected processViewInit(
		params: IDEPortalViewControllerParams<
			DEPortalViewActionType,
			IDEPortalViewAbility
		>
	) {
		super.processViewInit(params);
		Object.assign(this.store, { expandSearchForm: false, dataInfo: '' });
	}

	/**
	 * 获取数据看板能力
	 *
	 * @protected
	 * @return {*}  {(IDashboardAbility | undefined)}
	 * @memberof DEPortalViewController
	 */
	protected getMainCtrlAbility(): IDashboardAbility | undefined {
		const dashboard = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'DASHBOARD'
		);
		if (dashboard) {
			return this.getSubAbility<IDashboardAbility>(dashboard.name);
		}
		return undefined;
	}

	/**
	 * 获取搜索表单能力
	 *
	 * @protected
	 * @return {*}  {(ISearchFormAbility | undefined)}
	 * @memberof DEPortalViewController
	 */
	protected getSearchFormAbility(): ISearchFormAbility | undefined {
		const searchForm = this.model.ctrls.find(
			(ctrl: IParam) =>
				ctrl.controlType === 'SEARCHFORM' && ctrl.codeName !== 'QUICKSEARCHFORM'
		);
		if (searchForm) {
			return this.getSubAbility<ISearchFormAbility>(searchForm.name);
		}
		return undefined;
	}

	/**
	 * 视图挂载
	 *
	 * @param {IParam} [opts={}]
	 * @return {*}  {void}
	 * @memberof DEPortalViewController
	 */
	viewMounted(opts: IParam = {}): void {
		super.viewMounted(opts);
		if (this.model.useDefaultLayout) {
			if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
			this.load();
		} else {
			this.initLayout().then(() => {
				if (!this.isLoadDefault) {
					this.isLoadDefault = true;
					return;
				}
				this.load();
			});
		}
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof DEPortalViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const searchForm = this.getSearchFormAbility();
		const dashboard = this.getMainCtrlAbility();
		if (searchForm) {
			const tempViewParams = deepCopy(this.store.viewParams);
			return searchForm.loadDraft(tempViewParams);
		}
		if (dashboard) {
			const tempViewParams = deepCopy(this.store.viewParams);
			return dashboard.load(Object.assign(tempViewParams, args));
		} else {
			return Promise.reject({
				ok: false,
				data: null,
				rowData: { status: 500 },
			});
		}
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {(DashboardActionType | SearchFormActionType)} action
	 * @param {IParam[]} data
	 * @memberof DEPortalViewController
	 */
	handleCtrlAction(
		name: string,
		action: DashboardActionType | SearchFormActionType,
		data: IParam[]
	): void {
		if (name === 'dashboard') {
			this.handleDashboardAction(action as DashboardActionType, data);
		}
		if (name === 'searchForm') {
			this.handleSearchFormAction(action, data);
		}
	}

	/**
	 * 处理数据看板部件行为
	 *
	 * @protected
	 * @param {DashboardActionType} action
	 * @param {IParam[]} data
	 * @memberof DEPortalViewController
	 */
	protected handleDashboardAction(action: DashboardActionType, data: IParam[]) {
		if (action === 'load') {
			this.handleDashboardLoad(data);
		}
	}

	/**
	 * 处理数据看板数据
	 *
	 * @param {IParam[]} data
	 * @memberof DEPortalViewController
	 */
	protected async handleDashboardLoad(data: IParam[]) {
		const arg = data[0];
		this.store.dataInfo = this.model.majorPSAppDEField
			? arg[this.model.majorPSAppDEField.toLowerCase()]
			: App.ts('app.common.newlybuild', '新建');
	}

	/**
	 * 处理搜索表单行为
	 *
	 * @protected
	 * @param {SearchFormActionType} action
	 * @param {IParam[]} data
	 * @memberof DEPortalViewController
	 */
	protected handleSearchFormAction(
		action: SearchFormActionType,
		data: IParam[]
	) {
		const dashboard = this.getMainCtrlAbility();
		if (dashboard) {
			if (action === 'load') {
				const tempViewParams = deepCopy(this.store.viewParams);
				dashboard.load(tempViewParams);
			}
			if (action === 'search') {
				const tempViewParams = deepCopy(this.store.viewParams);
				dashboard.load(tempViewParams);
			}
		}
	}
}
